package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.dubbo.mappers.UserInfoMapper;
import com.tanhua.model.domain.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Slf4j
@DubboService
public class UserInfoApiImpl implements UserInfoApi {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public void save(UserInfo userInfo) {
        userInfoMapper.insert(userInfo);
    }

    @Override
    public void update(UserInfo userInfo) {
        userInfoMapper.updateById(userInfo);
    }

    @Override
    public UserInfo findById(Long userId) {
        LambdaQueryWrapper<UserInfo> lqw = new LambdaQueryWrapper<>();
        lqw.eq(UserInfo::getId, userId);
        return userInfoMapper.selectOne(lqw);
    }

    /**
     * 根据id和条件批量查询推荐用户信息
     *
     * @param userIds
     * @param userInfo
     * @return
     */
    @Override
    public Map<Long, UserInfo> findByIds(List<Long> userIds, UserInfo userInfo) {
        if(CollUtil.isEmpty(userIds)){
            return null;
        }
        log.info(userIds.toString());
        Set<Long> collect = userIds.stream().collect(Collectors.toSet());
        log.info(collect.toString());
        userIds = collect.stream().collect(Collectors.toList());
        log.info(userIds.toString());
//        LambdaQueryWrapper<UserInfo> lqw = new LambdaQueryWrapper<>();
//        lqw.eq(userIds != null, UserInfo::getId, collect);
//        if(userInfo!=null) {
//            lqw.eq(StringUtils.isEmpty(userInfo.getGender()), UserInfo::getGender, userInfo.getGender());
//            lqw.le(userInfo.getAge() != null, UserInfo::getAge, userInfo.getAge());
//        }
//        List<UserInfo> userInfos = userInfoMapper.selectList(lqw);
//        Map<Long, UserInfo> map = userInfos.stream().collect(Collectors.toMap(UserInfo::getId, item -> item));
//        log.info("详细用户信息{}", map);
//        根据id查询出所有的用户
        List<UserInfo> userInfos = userInfoMapper.selectBatchIds(userIds);
        Map<Long, UserInfo> map = new HashMap<>();
        for (UserInfo info : userInfos) {
//            if(info!=null&&info.getAge()<userInfo.getAge()&&info.getGender().equals(userInfo.getGender())){
//                map.put(info.getId(),info);
//            }
            map.put(info.getId(), info);
            if (info != null && userInfo != null) {
                if (userInfo.getAge() != null && userInfo.getAge() < info.getAge()) {
                    map.remove(info.getId());
                }
                if (userInfo.getGender() != null && !userInfo.getGender().equals(info.getGender())) {
                    map.remove(info.getId());
                }
            }
        }
        log.info("/*/*/*/*/*/-*/-/-*/-*/-*/-*用户详细信息{}", map.toString());
//        System.out.println(map);
//        Map<Long, UserInfo> collect = userInfos.stream().collect(Collectors.toMap(UserInfo::getId, item -> item));
        return map;
    }

    /**
     * 查询所有用户详情信息分页展示
     *
     * @param page
     * @param pagesize
     * @return
     */
    @Override
    public IPage<UserInfo> findAllPage(Integer page, Integer pagesize) {
        return userInfoMapper.selectPage(new Page<UserInfo>(page, pagesize), null);
    }

}